(* Copyright Per Lindgren 2014, see the file "LICENSE" *)
(* for the full license governing this code.           *)

(* RTFM-core/Cmd *)

open Common
open Options
  
let usage = 
  "The FUN-4Sure compiler v0.0, Per Lindgren (c) 2015" ^ nl ^ nl
    ^ "Usage [-v] [-D] [-gv_ecc file.gv] [-gv_if] [-why file.gv] [-why_il] [-d_ast] [-i] infile.st" ^ nl
    ^ "Recognized source file:" ^ nl
    ^ "infile.st        FB file is ST format" ^ nl
    ^ "Options summary:"

let o_verbose   = ref false
let o_debug     = ref false
let o_gv_if     = ref false
let o_why_il    = ref false
    
let f_infile    = ref ""
let f_dotfile   = ref ""
let f_whyfile   = ref ""
    
let d_ast       = ref false  
  
let speclist = 
  [
    ("-i", Arg.Set_string f_infile, "\t\t: infile");
    ("-gv_ecc", Arg.Set_string f_dotfile, "\t: graphviz file file.gv (default none)");
    ("-gv_if", Arg.Set o_gv_if, "\t: show if (default none)");
    ("-why", Arg.Set_string f_whyfile, "\t\t: whyML file file.mlw (default none)");
    ("-why_il", Arg.Set o_why_il, "\t: inline actions");
    ("-v", Arg.Set o_verbose, "\t\t: verbose mode (default disable)");
    ("-D", Arg.Set o_debug, "\t\t: debug mode (default disable)");      
    ("-d_ast", Arg.Set d_ast, "\t: dump AST");   
  ]
    
(* check if e is a file extension of s *)    
let ext s e =
  let le = String.length e in
  let ls = String.length s in    
  try
    compare (String.sub s (ls - le) le) e == 0  
  with
    _ -> false  
       
(* replace file extension e with r*)
let rep_ext s e r =
  let le = String.length e in
  let ls = String.length s in 
  String.sub s 0 (ls - le) ^ r
    
let cmd =
  (* let n o = if !o then 1 else 0 in (* count option *) *)
  (* Read the arguments *) 
  Arg.parse speclist (fun x -> f_infile := x) usage;
  try
  let check_ext name ex err = 
    if (not (ext name ex)) then raise (Arg.Bad(err ^ " " ^ name));
  in                                       
    (* infile *)
    if (String.compare !f_infile "" == 0) then raise (Arg.Bad("No infile selected"));
    check_ext !f_infile ".st" "Bad infile extention (.st exptected)";
    opt.infile <- !f_infile;
     
    (* general options *)
    opt.debug     <- ! o_debug;
    opt.verbose   <- ! o_verbose;
    
    (* additional options *)
    opt.gv_ecc <- (not (String.compare (!f_dotfile) "" == 0));
    let gv_ext = ".gv" in
    let gv_ext_err = "Bad Graphviz extension (.gv expected) " in
    if opt.gv_ecc then begin
      opt.gv_eccf <- !f_dotfile;
      check_ext opt.gv_eccf gv_ext gv_ext_err
    end;
    opt.gv_if     <- ! o_gv_if;
    
    opt.why <- (not (String.compare (!f_whyfile) "" == 0));
    let ext = ".mlw" in
    let ext_err = "Bad WhyML extension (.mlw expected) " in
    if opt.why then begin
      opt.whyf <- !f_whyfile;
      check_ext opt.whyf ext ext_err
    end;
    opt.why_il <- ! o_why_il;
    
    opt.d_ast  <- !d_ast;  
  with
    | Arg.Bad msg -> p_stderr ("Command line error: " ^ msg); exit (-1); 
